// pages/notice/notice/index.js
var dayjs = require('../../../utils/lib/dayjs.min.js');
const db = wx.cloud.database();
const TABLENAME = 'noticeDataList';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noticeDataList: [
      {
        _id: "123",
        isReadStatus: true,
        title: "录用通知",
        communication: "系统",
        time: dayjs(new Date()).format("YYYY-MM-DD HH:hh:ss"),
        minContent: "你申请的[茂南区家教]兼职己成功录用"
      },
      {
        _id: "456",
        isReadStatus: false,
        title: "录用通知",
        communication: "广东大土公司",
        time: dayjs(new Date()).format("YYYY-MM-DD HH:hh:ss"),
        minContent: "你申请的[日拿300周末网络临时工]兼职己成功录用"
      }
    ]
  },

  bindUrlToNoticeContent: function (e) {
    var _id = e.currentTarget.dataset.openid
    wx.navigateTo({
      url: '../noticeContent/noticeContent',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('noticeDataId', {
          data: _id
        })
      }
    })
  },

  bindSwithIsRead: function (e) {
    var _id = e.currentTarget.dataset.openid
    var listIndex = e.currentTarget.dataset.listindex
    var noticeDataList = this.data.noticeDataList
    noticeDataList[listIndex].isReadStatus = true

    this.setData({
      noticeDataList: noticeDataList
    });

    db.collection(TABLENAME).doc(_id).update({
      data: {
        isReadStatus: true
      },
      success: function(res) {
        console.log(res)
      }
    })

    // this.InsertWordData();
  },

  bindDeleteData : function (e) {
    var _id = e.currentTarget.dataset.openid
    var listIndex = e.currentTarget.dataset.listindex
    var noticeDataList = this.data.noticeDataList
    noticeDataList.splice(listIndex, 1)　

    this.setData({
      noticeDataList: noticeDataList
    });

    db.collection(TABLENAME).doc(_id).remove({
      success: function(res) {
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _openid = wx.getStorageSync('userOpenid').OPENID
    // this.InsertWordData()
    
    db.collection(TABLENAME).where({
      _openid: _openid
    })
    .get({
      success: res => {
        // res.data 是包含以上定义的两条记录的数组
        console.log(res.data)
        this.setData({
          noticeDataList:res.data
        })
      }
    })

  },

  InsertWordData: function () {
    try {
      db.collection(TABLENAME).add({
        data: {
          title: "录用通知",
          isReadStatus: false,
          communication: "广东大土公司",
          time: dayjs(new Date()).format("YYYY-MM-DD HH:hh:ss"),
          minContent: "你申请的[日拿300周末网络临时工]兼职己成功录用",
          content:"你申请的[茂南区家教]兼职己成功录用,恭喜您,加油"
        },
        success: function (res) {
          console.log(res)
        }
      })
    } catch (e) {
      console.error(e);
    }

  },

})